package cn.cslg.news.mail;

import java.net.MalformedURLException;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class NMail {

    private String mailhost;
    private String sender;
    private String senderName;
    private String username;
    private String password;
    private boolean mailAuth;

    public NMail(String mailhost, String sender, String senderName,
            String username, String password, boolean mailAuth) {
        this.mailhost = mailhost;
        this.sender = sender;
        this.senderName = senderName;
        this.username = username;
        this.password = password;
        this.mailAuth = mailAuth;
    }

    public void sendSimpleMail(String receiver, String receiverName,
            String subject, String content) throws EmailException {
        SimpleEmail email = new SimpleEmail();
        if (mailAuth) {
            email.setAuthentication(username, password);
        }
        email.setHostName(mailhost);
        email.addTo(receiver, receiverName);
        email.setFrom(sender, senderName);
        email.setCharset("gbk");
        email.setSubject(subject);
        email.setMsg(content);
        email.send();
    }

    public void sendHtmlMail(String receiver, String receiverName,
            String subject, String content) throws EmailException {
        HtmlEmail email = new HtmlEmail();
        if (mailAuth) {
            email.setAuthentication(username, password);
        }
        email.setHostName(mailhost);
        email.addTo(receiver, receiverName);
        email.setFrom(sender, senderName);
        email.setCharset("gbk");
        email.setSubject(subject);
        email.setMsg(content);
        email.send();
    }

    public void sendMultiPartMail(String receiver, String receiverName,
            String subject, String content, EmailAttachment attachment)
            throws EmailException {
        MultiPartEmail email = new MultiPartEmail();
        if (mailAuth) {
            email.setAuthentication(username, password);
        }
        email.setHostName(mailhost);
        email.addTo(receiver, receiverName);
        email.setFrom(sender, senderName);
        email.setCharset("gbk");
        email.setSubject(subject);
        email.setMsg(content);
        email.attach(attachment);
        email.send();
    }

    public EmailAttachment newAttachment(String path, String description,
            String name) throws MalformedURLException {
        EmailAttachment attachment = new EmailAttachment();
        attachment.setPath(path);
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setDescription(description);
        attachment.setName(name);
        return attachment;
    }
    
    public static void main(String[] args) throws EmailException{
        NMail m=new NMail("localhost", "jimshen@localhost", "jimshen", "jimshen", "123456",true);
        m.sendSimpleMail("jacky@localhost", "jacky", "test", "test java mail <a href='http://www.cslg.cn'>cslg</a>");
    }

}
